@echo off
setlocal enabledelayedexpansion

:: 检查参数输入
set IP=%~1
set PORT=%~2

:: 参数未提供时的交互模式
if "%IP%"=="" set /p "IP=Enter target IP address: "
if "%PORT%"=="" set /p "PORT=Enter port number: "

:: 输入验证
if "%IP%"=="" (
    echo Error: IP address cannot be empty!
    exit /b 1
)

if "%PORT%"=="" (
    echo Error: Port number cannot be empty!
    exit /b 1
)

:: 验证端口是否为数字
echo %PORT% | findstr /r "^[0-9]*$" >nul
if errorlevel 1 (
    echo Error: Invalid port number. Must be numeric!
    exit /b 1
)

:: 执行端口转发和防火墙规则
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=%PORT% connectaddress=%IP% connectport=%PORT%
netsh advfirewall firewall add rule name="Port %PORT% Forwarding" dir=in action=allow protocol=TCP localport=%PORT%

:: 显示结果
echo.
echo ===== Port forwarding configured =====
echo Target IP:   %IP%
echo Port:        %PORT%
echo ======================================
echo Run as administrator if commands failed
endlocal
